package com.dht.xiao.jianye.activity_bed;

import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.dht.xiao.jianye.R;
import com.dht.xiao.jianye.adapter.BedGetAdapter;
import com.dht.xiao.jianye.adapter.RecyclerAdapter;
import com.dht.xiao.jianye.base.BaseActivity;
import com.dht.xiao.jianye.bean.EndReadBean;
import com.dht.xiao.jianye.bean.SimpleBean;
import com.dht.xiao.jianye.dialog.SureNewDialog;
import com.dht.xiao.jianye.http.MainHttp;
import com.licheedev.myutils.LogPlus;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.utils.ToastUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class BedGetActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    BedGetAdapter mRecyclerAdapter;

    String ANDROID_ID;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_bed_get;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bed_get);
        ButterKnife.bind(this);
        mTitleBar.leftExit(this);
        initRecyclerView();
        getMachineCode();

    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRecyclerAdapter = new BedGetAdapter();
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        finishActivity();
    }

    private void getMachineCode() {
        ANDROID_ID =
                Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
        if (TextUtils.isEmpty(ANDROID_ID)) {
            ANDROID_ID = Build.SERIAL;
        }
        LogPlus.e("ANDROID_ID =" + ANDROID_ID);
        //ANDROID_ID = "6f4b9da9f38b5566";
       // seriesNum.setText(ANDROID_ID);

        opendoor(ANDROID_ID);

    }

    private void opendoor(String series) {
       // ToastUtil.show(getActivity(), "正在开门");
        MainHttp.get().opendoor(series)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<SimpleBean>() {
                    @Override
                    public void onSuccess(SimpleBean hotelBean) {
                        initHint();
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
    private void closedoor(String series) {
        MainHttp.get().closedoor(series)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<SimpleBean>() {
                    @Override
                    public void onSuccess(SimpleBean hotelBean) {
                        showOneToast(hotelBean.msg);
                        beginRead(ANDROID_ID);
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
    private void beginRead(String series) {

        MainHttp.get().beginRead(series,"0")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<SimpleBean>() {
                    @Override
                    public void onSuccess(SimpleBean hotelBean) {
                        showOneToast(hotelBean.msg);
                        delaytime();
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
    private void delaytime() {
        showWaitingDialog("正在读取",true);
        Observable.timer(5, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        dismissWaitingDialog();
                        endRead(ANDROID_ID);
                    }
                });

    }
    private void endRead(String series) {
        MainHttp.get().endRead(series,"2","0","")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<EndReadBean>() {
                    @Override
                    public void onSuccess(EndReadBean hotelBean) {
                        showOneToast(hotelBean.msg);
                        mRecyclerAdapter.setNewData(hotelBean.data.list);
                        finishHint();
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }



    private void initHint(){
        SureNewDialog dialog = new SureNewDialog(getActivity(), "投递", getString(R.string.open_hint));
        dialog.setOnCallBackListener(new SureNewDialog.OnCallBack() {
            @Override
            public void sure() {
                dialog.dismiss();
                closedoor(ANDROID_ID);
            }
        });
    }
    private void finishHint(){
        Toast.makeText(getActivity(),"已完成投递，系统5秒后退出",Toast.LENGTH_LONG).show();
        Observable.timer(5, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        // dialog.dismiss();
                        finishActivity();
                    }
                });
    }
}
